package shoppingmall.manage.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shoppingmall.manage.pojo.CommodityStandard;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.dto.Result;
import shoppingmall.manage.service.CommodityStandardService;

/**
 * 商品管理规格相关controller
 */
@Slf4j
@RestController
public class CommodityStandardController {

    @Autowired
    private CommodityStandardService commodityStandardService;

    /**
     * 分页查询所有的商品规格(page = ?  pageSize = ?)
     */
    @GetMapping("/manage/commodity/standard/all")
    public Result getCommodityStandardList (@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        log.info("查询商品规格列表,当前页:{},数据条数:{}",page,pageSize);
        //调用服务层方法...
        PageBean standardPageBean = commodityStandardService.getCommodityStandardList(page, pageSize);
        if(standardPageBean.getTotal() == 0) {
            return Result.error("未查询到商品规格!");
        }
        return Result.success(standardPageBean);
    }

    /**
     * 查询当前商品的规格(commodityId = ?)
     */
    @GetMapping("/manage/commodity/standard")
    public Result getCommodityStandardListByCommodityId (@RequestParam int commodityId) {
        log.info("查询商品规格列表:{}",commodityId);
        //调用服务层方法
        PageBean pageBean = commodityStandardService.getCommodityStandardListByCommodityId(commodityId);
        if(pageBean.getTotal() == 0) {
            return Result.error("未查询到当前商品规格!");
        }
        return Result.success(pageBean);
    }

    /**
     * 添加一条商品规格(指定规格对应的商品id,commodityId = ?)
     */
    @PostMapping("/manage/commodity/standard")
    public Result saveCommodityStandard (@RequestBody CommodityStandard commodityStandard) {
        log.info("添加一条商品:{},规格:{}",commodityStandard.getCommodityId(),commodityStandard.getCommodityAttr());
        //调用服务层方法
        boolean isSave = commodityStandardService.saveCommodityStandard(commodityStandard);
        if(!isSave){
            return Result.error("商品规格添加失败!");
        }
        return Result.success("商品规格添加成功!");
    }

    /**
     * 修改一条商品规格(通过规格id,id = ?)
     */
    @PutMapping("/manage/commodity/standard")
    public Result updateCommodityStandard (@RequestBody CommodityStandard commodityStandard) {
        log.info("修改一条商品规格:{}",commodityStandard.getId());
        //调用服务层方法
        boolean isUpdate = commodityStandardService.updateCommodityStandard(commodityStandard);
        if(!isUpdate){
            return Result.error("商品规格修改失败!");
        }
        return Result.success("商品规格修改成功!");
    }

    /**
     * 删除一条商品规格(通过规格id, id = ?)
     */
    @DeleteMapping("/manage/commodity/standard")
    public Result deleteCommodityStandard (@RequestParam int standardId) {
        log.info("删除一条商品规格:{}",standardId);
        //调用服务层方法
        boolean isDel = commodityStandardService.deleteCommodityStandard(standardId);
        if(!isDel){
            return Result.error("商品规格删除失败!");
        }
        return Result.success("商品规格删除成功!");
    }
}
